package com.xpoint.backend.controller;

import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.AddressBook;
import com.xpoint.backend.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class UserAddressController {
    @Autowired
    private UserAddressService addressService;

    @PostMapping
//    新增地址簿
    public R<String> insertAddressBook(@RequestBody AddressBook addressBook){
        return addressService.insertAddressBook(addressBook);
    }
    @GetMapping("/list")
//    查询所有地址簿
    public R<List<AddressBook>> findList(){
        return addressService.findAddressBookList();
    }

    @PutMapping("/default")
//    设置
    public R<String> upDefault(@RequestBody AddressBook addressBook){
        return addressService.upDefault(addressBook.getId());
    }

    @GetMapping("/default")
//    获取默认地址簿
    public R<AddressBook> getDefaultAddress(){
        return addressService.getDefaultAddress();
    }

    @GetMapping("/{id}")
//    根据id查询地址
    public R<AddressBook> findAddressBookById(@PathVariable Long id){
        return addressService.upAddressBookById(id);
    }

    @DeleteMapping
//    删除地址
    public R<String> deleteAddressBookById(@RequestParam("ids") Long id){
        return addressService.deleteAddressBookById(id);
    }

    @PutMapping
//    更新地址
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        return addressService.updateAddressBook(addressBook);
    }
}
